package com.example.mybatisplusdemo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.BusinessService;
import com.example.mybatisplusdemo.Service.OrderService;
import com.example.mybatisplusdemo.Service.UserService;
import com.example.mybatisplusdemo.pojo.Business;
import com.example.mybatisplusdemo.pojo.Order;
import com.example.mybatisplusdemo.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class OrderController {
    @Resource
    AdminService adminService;
    @Resource
    OrderService orderService;
    @Resource
    BusinessService businessService;
    @Resource
    UserService userService;


    /**
     * 管理员获取全部订单列表
     * @param token
     * @return
     */
    @GetMapping("/admingetorders")
    public Result admingetorders(@RequestParam("token") String token){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Order> list = orderService.list(new QueryWrapper<Order>().orderByDesc("id"));
        list.forEach(item->{
            item.setUsernike(userService.getOne(
                    new QueryWrapper<User>().
                            eq("id",item.getUserid())
            ).getNike());
            item.setBusnike(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())
            ).getNike());
        });
        return new Result(200,"查询成功",list);
    }

    /**
     * 商家查询其的订单
     * @param token
     * @param busid
     * @return
     */
    @GetMapping("/busgetorders")
    public Result busgetorders(@RequestParam("token") String token,@RequestParam("busid") String busid){
        if (!businessService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Order> list = orderService.list(new QueryWrapper<Order>().eq("busid",busid).orderByDesc("id"));
        list.forEach(item->{
            item.setUsernike(userService.getOne(
                    new QueryWrapper<User>().
                            eq("id",item.getUserid())
            ).getNike());
            item.setBusnike(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())
            ).getNike());
        });
        return new Result(200,"查询成功",list);
    }


    @PostMapping("/sendgood")
    public Result sendgood(@RequestParam("token") String token,@RequestParam("id") String id){
        if (!businessService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("id",id));
        order.setIssend("1");
        boolean isUpdate = orderService.update(order,new QueryWrapper<Order>().eq("id",id));
        if (isUpdate){return new Result(200,"已发货");}
        else{return new Result(400,"发货失败");}
    }

    /**
     * 获取用户订单
     * @param token
     * @param userid
     * @return
     */
    @GetMapping("/getorderbyid")
    public Result getorderbyid(@RequestParam("token") String token,
                               @RequestParam("userid") String userid){
        if (!userService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Order> list = orderService.list(new QueryWrapper<Order>().eq("userid",userid).orderByDesc("id"));
        list.forEach(item->{
            item.setUsernike(userService.getOne(
                    new QueryWrapper<User>().
                            eq("id",item.getUserid())
            ).getNike());
            item.setBusnike(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())
            ).getNike());
        });
        return new Result(200,"查询成功",list);
    }
}
